<?php
require_once '../config/config.php';
require_once 'inc/session.php';

$id_commande = $_GET['cmd'];

if (isset($_POST["submit"])) {
    foreach ($_POST as $key => $val)
        $_POST[$key] = mysql_real_escape_string($val);

    $id_etablissement = $_POST['id_etablissement'];
    $sql = 'UPDATE etablissement SET etablissement = "' . $_POST['etablissement'] . '", ville = "' . $_POST['ville'] . '", adresse = "' . $_POST['adresse'] . '", code_postal = "' . $_POST['code_postal'] . '", connaissance = "' . $_POST['connaissance'] . '" WHERE id = ' . $id_etablissement;
    mysql_query($sql) or die(mysql_error());

    $id_contact_administrative = $_POST['id_contact_administraive'];
    $sql = 'UPDATE contact_administratif SET nom = "' . $_POST['nom'] . '", prenom = "' . $_POST['prenom'] . '", fonction = "' . $_POST['fonction'] . '", telephone = "' . $_POST['telephone'] . '", email = "' . $_POST['email'] . '"';
    mysql_query($sql) or die(mysql_error());

    $sql = 'UPDATE commande SET commentaire = "' . $_POST['commentaire'] . '"';
    mysql_query($sql) or die(mysql_error());

    header("location:gestion_abonnes.php?maj=1");
    exit();
}

$menu = -1;

if (!isset($_GET['cmd'])) {
    header('Location:tableau_bord.php');
    exit();
}

$sql = 'SELECT id, id_etablissement, id_edition, id_abonnement, nb_beneficiares, date_creation, etat, commentaire FROM commande WHERE offre_essai = 0';
$req = mysql_query($sql) or die(mysql_error());
$res = mysql_fetch_array($req);

foreach ($res as $key => $val)
    $res[$key] = stripslashes($val);

$date_creation = date('d-m-Y H:i', strtotime($res['date_creation']));
$commentaire = $res['commentaire'];

//# etablissement
$id_etablissement = $res['id_etablissement'];
$sql_etablissement = "SELECT etablissement, ville, adresse, code_postal, connaissance FROM etablissement WHERE id = " . $id_etablissement;
$req_etablissement = mysql_query($sql_etablissement);
$res_etablissement = mysql_fetch_array($req_etablissement);

$etablissement = $res_etablissement['etablissement'];
$ville = $res_etablissement['ville'];
$adresse = $res_etablissement['adresse'];
$code_postal = $res_etablissement['code_postal'];

//# connaissance
switch ($res_etablissement['connaissance']) {
    case 1:
        $connaissance = "Par un moteur de recherche";
        break;
    case 2:
        $connaissance = "Par un abonné";
        break;
    case 3:
        $connaissance = "Par les réseaux sociaux";
        break;
    case 4:
        $connaissance = "Par la presse";
        break;
    case 5:
        $connaissance = "Par un emailing";
        break;
    case 6:
        $connaissance = "Par un contact avec l’équipe de l’Essentiel du sup";
        break;
    case 7:
        $connaissance = "Autre";
        break;
    default :
        $connaissance = "-";
        break;
}

//# contact administrative
$sql_contact = "SELECT id, nom, prenom, fonction, telephone, email FROM contact_administratif WHERE id_etablissement = " . $id_etablissement;
$req_contact = mysql_query($sql_contact);
$res_contact = mysql_fetch_array($req_contact);

$id_contact_administrative = ucwords($res_contact['id']);
$nom_contact = ucwords($res_contact['nom']);
$prenom_contact = ucwords($res_contact['prenom']);
$fonction_contact = $res_contact['fonction'];
$tel_contact = $res_contact['telephone'];
$email_contact = $res_contact['email'];

//# edition
switch ($res['id_edition']) {
    case 1:
        $edition = "Ecoles de management";
        break;
    case 2:
        $edition = "Ecoles d’ingénieurs";
        break;
    case 3:
        $edition = "Universités";
        break;
}

//# nb beneficiares
$sql_beneficiare = "SELECT id FROM beneficiare WHERE id_commande = " . $id_commande;
$req_beneficiare = mysql_query($sql_beneficiare);
$nb_beneficiares = mysql_num_rows($req_beneficiare);

//# statut
switch ($res['etat']) {
    case 0:
        $statut = "En cours";
        break;
    case 1:
        $statut = "Activé";
        break;
    case 2:
        $statut = "Désactivé";
        break;
    default:
        break;
}

if (isset($_GET['ajt']))
    $message_succes = 'Bénéficiaire ajouté avec succès.';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Interface Administration | L'essentiel du sup</title>
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link rel="shortcut icon" href="images/favicon.ico" />
        <link rel="stylesheet" href="css/style.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery/jquery-ui-1.9.2.custom.min.css" />
        <!--[if IE 9]>
            <link rel="stylesheet" media="screen" href="css/ie9.css"/>
        <![endif]-->
        <!--[if IE 8]>
            <link rel="stylesheet" media="screen" href="css/ie8.css"/>
        <![endif]-->
        <!--[if IE 7]>
            <link rel="stylesheet" media="screen" href="css/ie7.css"/>
        <![endif]-->
        <style type="text/css">
            .filter .date-filter {
                height: 30px;
            }

            .filter .status-filter {
                height: 30px;
            }

            .filter .status-filter label {
                margin: 2px 20px 0 1px; 
                vertical-align: middle
            }

            .tableoptions .ui-datepicker-trigger {
                margin: 1px 0 0 3px;
                padding: 0;
                vertical-align: top;
            }

            div.ui-datepicker{ font-size:11px; }

            .date-pick {
                border: 1px solid #CCCCCC;
                padding: 4px;
            }
        </style>
        <script type="text/javascript" src="js/plugins/jquery-1.7.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.flot.resize.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.alerts.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.effects.core.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.effects.explode.min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.colorbox-min.js"></script>
        <script type="text/javascript" src="js/plugins/jquery.ui.datepicker-fr.js"></script>
        <script type="text/javascript" src="js/custom/general.js"></script>
        <script type="text/javascript" src="js/custom/tables.js"></script>
        <script type="text/javascript" src="js/custom/media.js"></script>
        <!--[if lt IE 9]>
                <script src="js/css3-mediaqueries.js"></script>
        <![endif]-->
        <script type="text/javascript">
            jQuery(document).ready(function() {

                jQuery("#btn_retour").click(function(e) {
                    e.preventDefault();
                    var url = 'gestion_visiteurs.php';
                    window.location.href = url;
                });

                jQuery("#btn_ajouter").click(function(e) {
                    e.preventDefault();

<?php if ($nb_beneficiares == 20) { ?>
                        if (num_checked > 1) {
                            jAlert("Le nombre maximale de bénificiares est atteint!", 'Attention');
                            return false;
                        }
<?php } else { ?>
                        var url = 'ajouter_beneficiare.php?cmd=<?php echo $id_commande ?>';
                        window.location.href = url;
<?php } ?>
                });
            });
        </script>
    </head>
    <body class="loggedin">
        <?php include('inc/header.php') ?>
        <div class="mainwrapper">
            <div class="mainwrapperinner">
                <?php include('inc/menu.php') ?>
                <div class="maincontent noright">
                    <div class="maincontentinner">
                        <ul class="maintabmenu">
                            <li class="current">
                                <a>Modifier Abonnement</a>
                            </li>
                        </ul>
                        <div class="content">
                            <div style="margin: 0 0 0 20px; float: right;">
                                <a id="btn_ajouter" class="btn btn_add radius50" style="background-color: rgb(247, 247, 247);" href="#">
                                    <span>Ajouter un bénéficiaire</span>
                                </a>
                            </div>
                            <br/>
                            <br/>
                            <?php if (!empty($message_succes)) {
                                ?>
                                <div class="notification msgsuccess">
                                    <a class="close"></a>
                                    <p><?php echo $message_succes; ?></p>
                                </div>
                            <?php } ?>
                            <?php if (!empty($message_erreur)) {
                                ?>
                                <div class="notification msgerror">
                                    <a class="close"></a>
                                    <p><?php echo $message_erreur ?></p>
                                </div>
                            <?php } ?>
                            <form class="stdform stdform2" action="modifier_abonne.php?cmd=<?php echo $id_commande ?>" method="post">
                                <input type="hidden" name="id_etablissement" value="<?php echo $id_etablissement ?>" />
                                <input type="hidden" name="id_contact_administraive" value="<?php echo $id_contact_administrative ?>" />
                                <div class="contenttitle">
                                    <h2 class="form"><span>Informations sur l’abonnement</span></h2>
                                </div>
                                <p>
                                    <label>Etablissement :</label> 
                                    <span class="field">
                                        <input type="text" name="etablissement" class="smallinput" value="<?= $etablissement ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Ville :</label> 
                                    <span class="field">
                                        <input type="text" name="ville" class="smallinput" value="<?= $ville ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Adresse :</label> 
                                    <span class="field">
                                        <input type="text" name="adresse" class="smallinput" value="<?= $adresse ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Code postal :</label> 
                                    <span class="field">
                                        <input type="text" name="code_postal" class="smallinput" value="<?= $code_postal ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Edition :</label> 
                                    <span class="field">
                                        <select name="edition">
                                            <option value="1" <? if ($edition == 1) { ?> selected <? } ?>>Ecoles de management</option>
                                            <option value="2" <? if ($edition == 2) { ?> selected <? } ?>>Ecoles d’ingénieurs</option>
                                            <option value="3" <? if ($edition == 3) { ?> selected <? } ?>>Universités</option>
                                        </select>
                                    </span>
                                </p>
                                <p>
                                    <label>Nombre de bénéficiaires :</label> 
                                    <span class="field">
                                        <input disabled type="text" name="nb_benificiaires" class="smallinput" value="<?= $nb_beneficiares ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Statut :</label>
                                    <span class="field"><?php echo $statut ?></span>
                                </p>
                                <p>
                                    <label>Comment avez-vous connu l’Essentiel du sup ? :</label> 
                                    <span class="field">
                                        <select name="connaissance">
                                            <option value="1" <? if ($connaissance == 1) { ?> selected <? } ?>>Par un moteur de recherche</option>
                                            <option value="2" <? if ($connaissance == 2) { ?> selected <? } ?>>Par un abonné</option>
                                            <option value="3" <? if ($connaissance == 3) { ?> selected <? } ?>>Par les réseaux sociaux</option>
                                            <option value="4" <? if ($connaissance == 4) { ?> selected <? } ?>>Par la presse</option>
                                            <option value="5" <? if ($connaissance == 5) { ?> selected <? } ?>>Par un emailing</option>
                                            <option value="6" <? if ($connaissance == 6) { ?> selected <? } ?>>Par un contact avec l’équipe de l’Essentiel du sup</option>
                                            <option value="7" <? if ($connaissance == 7) { ?> selected <? } ?>>Autre</option>
                                        </select>
                                    </span>
                                </p>
                                <p class="stdformbutton">
                                    <button name="submit" class="submit radius2" type="submit">Enregistrer</button>
                                    <input type="reset" class="reset radius2" value="Annuler" />
                                </p>
                                <br/>
                                <div class="contenttitle">
                                    <h2 class="form"><span>Informations sur le contact administratif</span></h2>
                                </div>
                                <p>
                                    <label>Prénom :</label> 
                                    <span class="field">
                                        <input type="text" name="prenom" class="smallinput" value="<?= $prenom_contact ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Nom :</label> 
                                    <span class="field">
                                        <input type="text" name="nom" class="smallinput" value="<?= $nom_contact ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Fonction :</label> 
                                    <span class="field">
                                        <input type="text" name="fonction" class="smallinput" value="<?= $fonction_contact ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>Téléphone :</label> 
                                    <span class="field">
                                        <input type="text" name="telephone" class="smallinput" value="<?= $tel_contact ?>" />
                                    </span>
                                </p>
                                <p>
                                    <label>E-mail :</label> 
                                    <span class="field">
                                        <input type="text" name="email" class="smallinput" value="<?= $email_contact ?>" />
                                    </span>
                                </p>
                                <p class="stdformbutton">
                                    <button name="submit" class="submit radius2" type="submit">Enregistrer</button>
                                    <input type="reset" class="reset radius2" value="Annuler" />
                                </p>
                                <br/>
                                <div class="contenttitle">
                                    <h2 class="form"><span>Commentaire Administrateur</span></h2>
                                </div>
                                <p>
                                    <label>Commentaire :</label> 
                                    <span class="field">
                                        <textarea name="commentaire" rows="4" cols="150" ><?php echo $commentaire ?></textarea>
                                    </span>
                                </p>
                                <p class="stdformbutton">
                                    <button name="submit" class="submit radius2" type="submit">Enregistrer</button>
                                    <input type="reset" class="reset radius2" value="Annuler" />
                                </p>
                            </form>
                            <br clear="all" />
                        </div>
                    </div>
                    <div class="footer">
                        <p></p>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>
